"""Script to parse dbpedia triples from a file and generate an index file
that can be used later for seeking to the right place.
"""
import re, sys, time, pprint


def main(N, type, fname):
   start = time.time()
   if type=='instances':
      r = re.compile(r'<http://dbpedia\.org/resource/([^>]+)>\s*(<[^\]]+>)\s*(<[^\]]+>) \.')
   else:
      r = re.compile(r'<http://dbpedia\.org/resource/([^>]+)>\s*(<[^\]]+>)\s*"(.+?)"@en \.')
   offsets = {}
   i=0
   with open(fname, "r", 1) as f:
      pos = f.tell()
      line = f.readline()
      while line:
         i += 1
         if i%10000==0:
            sys.stderr.write('.')
         if i>=N:
            break
         m = r.match(line)
         if m:
            offsets[m.groups()[0]] = pos
         pos = f.tell()
         line = f.readline()
   sys.stderr.write('took %f\n' % (time.time()-start))
   print '%s = \\' % type
   pprint.pprint(offsets)

if __name__=='__main__':
   assert(len(sys.argv)==4)
   N = int(sys.argv[1])
   type = sys.argv[2]
   fname = sys.argv[3]
   main(N, type, fname)
